const app = getApp()
let qCloud = require("../../../index.js");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgPath: qCloud.config.STATIC_IMAGE_PATH,
        total: 0,
        pageNum: 1,
        pageSize: 10,
        deptList: [],
    },

    radioChange(e) {
        let items = this.data.deptList
        let value = e.detail.value
        for (let i = 0, lenI = items.length; i < lenI; ++i) {
            if (items[i].deptId == value) {
                wx.setStorageSync("weapp_choose_dept", {
                    deptId: items[i].deptId,
                    deptName: items[i].deptName
                });
                break;
            }
        }
    },

    doSubmit() {
        wx.navigateBack({
            delta: 1
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getDeptListAll();
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        let self = this;
        self.setData({
            pageNum: 1,
            pageSize: 10,
            deptList: []
        });
        self.getDeptListAll();
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        let self = this;
        if (self.data.pageSize == self.data.deptList.length) {
            self.setData({
                pageNum: self.data.pageNum + 1
            });
            self.getDeptListAll();
        } else {
            return;
        }
    },

    getDeptListAll: function () {
        let self = this;
        let params = {
            deptId: qCloud.session.getUser().organId
        };

        app.fetchApis(self, qCloud.api.getDeptListAll, params, "GET", function (e) {
            if (e.data.code == 200) {
                self.setData({
                    deptList: e.data.data
                })
            }
        })
    }
})
